package com.graduation.sports.db;

import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.graduation.sports.bean.CommunityInfo;
import com.graduation.sports.bean.DailyExerciseInfo;
import com.graduation.sports.bean.GroupInfo;
import com.graduation.sports.util.DateUtils;

import org.litepal.LitePal;

import java.util.Random;

/**
 * <pre>
 *     author : Lucien Z
 *     e-mail : 825038797@qq.com
 *     time   : 2019/10/30
 *     desc   : 管理所有的数据库操作,包括初始化数据库，更新数据库，删除数据库
 *     version: 1.0
 * </pre>
 */
public class BaseDBHelper {
    private static final String TAG = BaseDBHelper.class.getSimpleName();
    private static final String IS_FIRST_INIT = "IS_FIRST_INIT";
    private volatile static BaseDBHelper sDataBaseHelper;

    public static BaseDBHelper getInstance() {
        if (sDataBaseHelper == null) {
            synchronized (BaseDBHelper.class) {
                if (sDataBaseHelper == null) {
                    sDataBaseHelper = new BaseDBHelper();
                }
            }
        }
        return sDataBaseHelper;
    }

    public int getLitePalDBVersion() {
        return LitePal.getDatabase().getVersion();
    }

    public boolean isFirstInit() {
        boolean isFirstInit = SPUtils.getInstance().getBoolean(IS_FIRST_INIT, true);
        if (isFirstInit) {
            SPUtils.getInstance().put(IS_FIRST_INIT, false);
            return true;
        }
        return false;
    }

    public void initTextData() {
        for (int i = 0; i < 8; i ++ ){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setGroupName("小组" + i);
            groupInfo.setGroupImg("http://www.ico51.cn/images/logo.png");
            boolean result = groupInfo.save();
            LogUtils.d("GroupInfo insert result " + result);
        }

        for (int j = 0; j < 20; j++) {
            CommunityInfo communityInfo = new CommunityInfo();
            communityInfo.setContent("我是发布的消息" + j);
            communityInfo.setTimeStamp(DateUtils.convertToString(System.currentTimeMillis()));
            communityInfo.setUserAccount("aaa");
            communityInfo.setUserName("张三" + j);
            boolean result = communityInfo.save();
            LogUtils.d("CommunityInfo insert result " + result);

        }
        String[] names = new String[]{"跳绳","坐位体前屈","原地摸高"};
        for (int i = 0; i < names.length; i++) {
            DailyExerciseInfo exerciseInfo = new DailyExerciseInfo();
            exerciseInfo.setTimestamp(DateUtils.formatStringDateShort(2021,3,3));
            exerciseInfo.setTimes(i + 1);
            exerciseInfo.setExerciseTime("21:34");
            exerciseInfo.setSportName(names[i]);
            exerciseInfo.save();
        }
    }


}